package com.jml.zlhq.app.ui;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;
import com.loopj.android.http.TextHttpResponseHandler;

public class BindZfbActivity extends BaseActivity{
	private EditText userid,zfb;
	private TextView msg;
	private Button submit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_zfb_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		//LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		//rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("绑定支付宝");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("  ");
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		msg = (TextView) findViewById(R.id.bind_zfb_msg);
		userid = (EditText) findViewById(R.id.bind_zfb_xm);
		zfb = (EditText) findViewById(R.id.bind_zfb_zh);
		
		submit = (Button) findViewById(R.id.bind_zfb_submit);
		submit.setOnClickListener(this);
	}

	@Override
	public void loadData(Map<String, String> param) {}

	@Override
	public void clearData() {}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==201&&appContext.isLogin()){
			finish();	
		}
		if(requestCode==202&&appContext.isLogin()){
			finish();	
		}
		if(requestCode==203&&appContext.isLogin()){
			UIUtil.openActivity(activity, BindPhoneActivity.class, null,204);
		}
		if(requestCode==204&&appContext.isLogin()){
			finish();
		}
	}

	@Override
	public void onClick(View view) {
		msg.setText("");
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.bind_zfb_submit:
			String xm = userid.getText().toString();
			String id = zfb.getText().toString();
			if(StringUtil.isEmpty(xm)||StringUtil.isEmpty(id)){
				msg.setText("支付宝账号和姓名不能为空！");
			}else{
				Map<String, String> param = new HashMap<String, String>();
				param.put("auth_token", appContext.getLoginUid());
				param.put("alipay_name", id);
				param.put("real_name", xm);
				final Handler mHandler = new Handler(){
					@Override
					public void handleMessage(Message msg) {
						final CommonDialog show = new CommonDialog(activity, true);
						switch (msg.what) {
						case 0:
							show.show(true, true, false, "提示", "成功，成功绑定支付宝！", "确定", "", new OnClickListener(){
								@Override
								public void onClick(View v) {
									LoginActivity.afterLogined(activity, null);
									show.dismiss();
									setResult(Activity.RESULT_OK);
									activity.finish();
								}
							}, null);
							break;
						
						default:
							show.show(true, true, false, "提示", "失败，系统出错，请联系管理员！", "确定", "", new OnClickListener(){
								@Override
								public void onClick(View v) {
									show.dismiss();
								}
							}, null);
							break;
						}
					}
				};
				HttpService.postByText(URLs.MAIN_USER_BIND_ZFB, param, new TextHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, Header[] arg1, String resultStr) {
						try {
							JSONObject result = new JSONObject(resultStr);
							mHandler.sendEmptyMessage(result.getInt("status"));
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
						UIUtil.ToastMessage(activity, "抱歉，访问失败");
					}
				});
			}
			break;
		default:
			break;
		}
	}
}
